package com.atguigu.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

public class NettyByteBuf02 {

    public static void main(String[] args) {
        ByteBuf buf = Unpooled.copiedBuffer("hello,world,北京", CharsetUtil.UTF_8);

        // 使用api
        if (buf.hasArray()) {
            byte[] content = buf.array();

            System.out.println(new String(content,CharsetUtil.UTF_8));

            System.out.println("ByteBuf：" + buf);

            System.out.println(buf.arrayOffset());

            System.out.println(buf.readerIndex());

            System.out.println("writerIndex: " + buf.writerIndex());

            System.out.println(buf.capacity());

//            System.out.println(buf.readByte()); // 可读取的字节数
            System.out.println(buf.getByte(0));

            int len = buf.readableBytes();
            System.out.println(len); // 可读取的字节数

            for (int i = 0; i < len; i++) {
                System.out.println((char) buf.getByte(i));
            }

            System.out.println(buf.getCharSequence(0,4,CharsetUtil.UTF_8));
        }
    }
}
